package com.shenjinzheng.sso.service.impl.member;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenjinzheng.sso.dto.MemberLoginDTO;
import com.shenjinzheng.sso.entity.Member;
import com.shenjinzheng.sso.mapper.MemberMapper;
import com.shenjinzheng.sso.service.MemberAuthService;
import com.shenjinzheng.sso.service.MemberService;
import com.shenjinzheng.sso.utils.JwtUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Slf4j
@Service
public class MemberServiceImplByPhone extends ServiceImpl<MemberMapper, Member> implements MemberAuthService {
    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private MemberService memberService;

    @Override
    public String login(MemberLoginDTO memberLoginDTO) {
        log.info("账号密码登录方式....");
        Member member = memberService.lambdaQuery()
                .eq(Member::getPhone, memberLoginDTO.getPhone())
                .one();
        //判断该账号未注册的情况
        if (member == null) {
            throw new RuntimeException("账号或者密码错误");
        }
        //判断用户输入的密码不正确的情况
        if (!member.getPassword().equals(memberLoginDTO.getPassword())) {
            throw new RuntimeException("账号或者密码错误");
        }
        //都正确 -> 签发token
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", member.getId());
        return jwtUtil.createToken(map);
    }

    @Override
    public String getCode(String phone) {
        throw new RuntimeException("账号密码登录不需要验证码");
    }
}
